
import { _decorator, Collider, Component, ITriggerEvent, Node } from 'cc';
import { GameManager } from '../framework/GameManager';
import { Constant } from '../framework/Constant';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = BulletProp
 * DateTime = Tue Mar 12 2024 15:21:54 GMT+0800 (中国标准时间)
 * Author = kun12303
 * FileBasename = BulletProp.ts
 * FileBasenameNoExtension = BulletProp
 * URL = db://assets/script/bullet/BulletProp.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */
 
@ccclass('BulletProp')
export class BulletProp extends Component {
    private _propSpeed = 0.3;
    private _propXSpeed = 0.3;
    private _gameManager:GameManager = null;

    start () {
        // [3]
    }

    protected onEnable(): void {
        const collider = this.getComponent(Collider);
        collider.on('onTriggerEnter',this._onTriggerEnter,this)
    }

    protected onDisable(): void {
        const collider = this.getComponent(Collider);
        collider.off('onTriggerEnter',this._onTriggerEnter,this)
    }

    update (deltaTime: number) {
        let pos = this.node.position;
        if(pos.x>=15){
            this._propXSpeed = this._propSpeed
        }else if(pos.x<=-15){
            this._propXSpeed = -this._propSpeed
        }
        this.node.setPosition(pos.x+this._propXSpeed,pos.y,pos.z-this._propSpeed)
        pos = this.node.position
        if(pos.z>50){
            PoolManager.instance().putNode(this.node);
            // this.node.destroy()
        }
    }

    show(gameManager:GameManager,speed:number){
        this._gameManager = gameManager
        this._propSpeed = speed
    }

    private _onTriggerEnter(event:ITriggerEvent){
        const name = event.selfCollider.node.name;
        if(name === 'bulletH'){
            this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_H)
        }else if(name === 'bulletS'){
            this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_S)
        }else{
            this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_M)
        }
        PoolManager.instance().putNode(this.node);
        // this.node.destroy();
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
